package com.businessportal.web.controllers.blog;

import com.businessportal.web.dataaccess.entities.BlogEntity;
import com.businessportal.web.dataaccess.entities.Login;
import com.businessportal.web.dataaccess.services.BlogEntityService;
import com.businessportal.web.dataaccess.services.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Vadim on 7/2/14.
 */

@WebServlet(urlPatterns = "/publicpages/blog_viewer.do")
public class BlogViewerController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        try {
            req.setCharacterEncoding("UTF-8");

            String showBogId = req.getParameter("showBogId");

            Login user = UserService.INSTANCE.CheckLoginPass("login0", "Password");

            if(null != showBogId && !showBogId.isEmpty()){

                BlogEntity blogToShow = BlogEntityService.INSTANCE.getBlogById(Long.parseLong(showBogId));

                req.setAttribute("blogid", blogToShow.getId());
                req.setAttribute("blogtitle", blogToShow.getBlogTitle());
                req.setAttribute("blogtext", blogToShow.getBlogText());
            } else {
                req.setAttribute("blogid", "");
                req.setAttribute("blogtitle", "");
                req.setAttribute("blogtext", "");
                throw new Exception("Not found");
            }

        } catch(Exception e){
            req.setAttribute("errors", e.getMessage());
        }

        getServletContext().getRequestDispatcher("/publicpages/blog_viewer.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
